package com.lenovo.smarttraffic.zm_user_module;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.content.Intent;
import android.content.res.Resources;
import android.graphics.Color;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
import android.widget.ImageView;

import com.lenovo.smarttraffic.R;
import com.lenovo.smarttraffic.bean.PeccancyInfoBean;

import static com.lenovo.smarttraffic.util.CommonUtil.getResources;

public class ZmPeccancyItemAdapter extends BaseAdapter {
    String TAG = "ZmPeccancyItemAdapter";

    private List<UserPeccancyBean> objects = new ArrayList<UserPeccancyBean>();

    private Context context;
    private LayoutInflater layoutInflater;

    public ZmPeccancyItemAdapter(Context context, List<UserPeccancyBean> objects) {
        this.context = context;
        this.layoutInflater = LayoutInflater.from(context);
        this.objects = objects;
    }

    @Override
    public int getCount() {
        return objects.size();
    }

    @Override
    public UserPeccancyBean getItem(int position) {
        return objects.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (convertView == null) {
            convertView = layoutInflater.inflate(R.layout.zm_peccancy_item, null);
            convertView.setTag(new ViewHolder(convertView));
        }
        initializeViews((UserPeccancyBean) getItem(position), (ViewHolder) convertView.getTag(), position);
        return convertView;
    }

    private void initializeViews(UserPeccancyBean object, ViewHolder holder, int position) {
        holder.peccancyTvNum.setText(position + "");
        holder.peccancy_tv_carNumber.setText("" + object.getCarnumber());
        holder.peccancyTvPaddr.setText("" + object.getPaddr());
        holder.peccancyTvPRemarks.setText("" + object.getPremarks());
        holder.peccancyTvPScore.setText("" + object.getPscore());
        holder.peccancyTvPMoney.setText("" + object.getPmoney());
        holder.peccancyTvPDatetime.setText("" + object.getPdatetime());
        if (object.getHanlderFlag()) {
            Log.e(TAG, "initializeViews: " + "=====");
            holder.peccancy_tv_state.setText("已处理");
            holder.peccancy_tv_state.setTextColor(Color.parseColor("#FF8BC34A"));
        } else {
            holder.peccancy_tv_state.setText("未处理");
        }
        holder.peccancy_tv_state.setEnabled(true);
        holder.peccancy_tv_state.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(context, QRcodeActivity.class);
                intent.putExtra("listName", object.getUsername());
                intent.putExtra("position", position);
                context.startActivity(intent);
            }
        });
        if (object.getPmoney() == 0) {
            holder.peccancy_tv_state.setEnabled(false);
        }

        String carbrand = object.getCarbrand();

        int mipmapResource = getMipmapResource(carbrand);
        holder.peccancyIvCarBrand.setImageResource(mipmapResource);


    }

    public int getMipmapResource(String imageName) {
//   这个时drawable
//        Class drawable = R.drawable.class;
//        Field field = null;
//        try {
//            field = drawable.getField(carbrand);
//            int res_ID = field.getInt(field.getName());
//            holder.peccancyIvCarBrand.setImageResource(res_ID);
//        } catch (Exception e) {
//
//        }
        Class mipmap = R.mipmap.class;
        try {
            Field field = mipmap.getField(imageName);
            int resId = field.getInt(imageName);
            return resId;
        } catch (NoSuchFieldException e) {//如果没有在"mipmap"下找到imageName,将会返回0
            return 0;
        } catch (IllegalAccessException e) {
            return 0;
        }
    }

    protected class ViewHolder {
        private TextView peccancyTvNum;
        private ImageView peccancyIvCarBrand;
        private TextView peccancyTvPaddr;
        private TextView peccancyTvPRemarks;
        private TextView peccancyTvPScore;
        private TextView peccancyTvPMoney;
        private TextView peccancyTvPDatetime, peccancy_tv_carNumber, peccancy_tv_state;

        public ViewHolder(View view) {
            peccancyTvNum = (TextView) view.findViewById(R.id.peccancy_tv_num);
            peccancyIvCarBrand = (ImageView) view.findViewById(R.id.peccancy_iv_carBrand);
            peccancyTvPaddr = (TextView) view.findViewById(R.id.peccancy_tv_paddr);
            peccancyTvPRemarks = (TextView) view.findViewById(R.id.peccancy_tv_pRemarks);
            peccancyTvPScore = (TextView) view.findViewById(R.id.peccancy_tv_pScore);
            peccancyTvPMoney = (TextView) view.findViewById(R.id.peccancy_tv_pMoney);
            peccancyTvPDatetime = (TextView) view.findViewById(R.id.peccancy_tv_pDatetime);
            peccancy_tv_carNumber = view.findViewById(R.id.peccancy_tv_carNumber);
            peccancy_tv_state = view.findViewById(R.id.peccancy_tv_state);
        }
    }
}
